<!--
* description:短信发送弹窗
* author: zxuan
* createdDate: 2019-11-12
-->
<template>
  <section class="sendMsg">
    <el-dialog v-dialogDrag
      :close-on-click-modal="false"
      title="短信发送"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      @close="sendCode"
      width="1000px"
    >
      <dialogHeader
        slot="title"
        :title="'短信发送'"
        :styleType="$store.state.app.dialogType"
      ></dialogHeader>
      <div class="filter-container filter-params">
        <el-row :gutter="26">
          <component
            v-for="comp in tableComponents.filter(o => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire+ comp.compKey : comp.compKey"
            :validrule="comp.validrule"
            :key="comp.compKey"
            :codeField="comp.codeField"
            :textField="comp.textField"
            :popupsKey="comp.compKey"
            :is="comp.component"
            :isMul="comp.isMul"
            :code="formField[comp.codeField]"
            :inputType="comp.inputType"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
            :disabled="comp.disabled"
            :isRequire="comp.isRequire"
            :span="comp.span || 8"
            :dateType="comp.dateType"
            :labelName="comp.labelName"
            :lookuptype="comp.lookuptype"
            :dateOptionsType="comp.dateOptionsType"
          ></component>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          @click="comp.clickEvent"
        >{{comp.text}}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from "@/components/mixins/formMixins";
import { crmApis } from "@/api/apiList/crm";
import OneTableTemplate from "@/components/crm/commonComponents/crmPopupsOneTable";
export default {
  // 组件混入对象
  mixins: [formMixins],
   components: {
    dialogHeader: () => import("@/components/dialogHeader") //弹窗标题
  },
  data() {
    return {
      curPopupsVisible:false,
      // 保存API配置对象
      apiConfig: crmApis,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey3",
          type: "primary",
          size: "small",
          clickEvent: () => this.sendMsg(),
          text: "发送",
          position: "bottom"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "compKey1",
          labelName: "接收人手机",
          codeField: "phone",
          component: () => import("@/components/crm/textbox/crmTextInput"),
          isRequire:true,
          type: "inputText",
          isMust: true,
        },
        {
          compKey: "compKey2",
          labelName: "短信模板",
          codeField: "templateName",
          textField: "templateName",
          isRequire: true,
          component: () =>
            import("@/components/crm/crmEjectWindows/crmsendMsg"),
          type: "propus",
          isMust: true,
          isMul: false,
          emitCode:(row)=>this.msgChange(row)
        },
        {
          compKey: "compKey3",
          labelName: "模板内容",
          codeField: "templateContent",
          textField: "templateContent",
          component: () => import("@/components/crm/textbox/crmTextInput"),
          isRequire:true,
          inputType: "textarea",
          isMust: true,
          span:24
        }

      ],
      // 标题
      textMap: {
        edit: "短信模板",
        add: "短信模板"
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        phone: "",
        templateName: "",
        templateContent:"",
        templateId:"",
      },
    };
  },
  methods: {
    msgChange(row){
      console.log(row)
    },
    //保存
    sendMsg() {
      console.log(this.formField.templateId)
      var html=""
      let that=this;
      if(that.formField.phone=="")
      {
        html = html + `<div class="sys-alertDiv">请输入接收人手机</div>`
      }
      if(that.formField.templateId=="")
      {
        html = html + `<div class="sys-alertDiv">请选择短信模板</div>`
      }
      if(that.formField.templateContent=="")
      {
        html = html + `<div class="sys-alertDiv">模板内容不能为空</div>`
      }
      if (html != '') {
         this.$crmcf.showMessages(this, 'error', html, true)
        return;
      }
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: crmApis.csBuMessSendMutationSaveMore,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          // 当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo: {
            templateId:that.formField.templateId,
            phone:that.formField.phone,
            msgContent:that.formField.templateContent,
            bussOwner:this.$store.getters.orgInfo.DLR_CODE
          }
        }
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          // 保存成功
          this.$message({
            message: '保存成功',
            type: 'success',
            uration: 1000
          })
          this.curPopupsVisible=false
        } else {
          // 保存失败
          // this.sendCode('0')
          this.$message({
            message: '保存失败：' + response.msg,
            type: 'warn',
            uration: 1000
          })
        }
      })

    }
  }
};
</script>
